// pages/index/AEDmap/AEDmap.js
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude: 113.324520,
    latitude: 23.099994,
    markers: [{
      id: 0,
      //图标
      iconPath: "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/mapicon/location_green.png?sign=267bfe180042f24a85eb0c69e855c62d&t=1618307898",
      latitude: 23.099994, //经度
      longitude: 113.324520, //纬度
      width: 50,
      height: 50
    }],
    modalHidden: true, //是否隐藏搜索弹窗
    showMarkers: false, //是否启用搜索功能
    mapCtx: null //地图组件
  },
  toSearch() {
    var that = this;
    this.onLoad();
    this.setData({
      modalHidden: false
    })
    //that.setMarkers(null);
    setTimeout(
      function () {
        console.log("等待2秒，优化搜索体验")
        wx.cloud.callFunction({
          name: "getAEDposition",
          data: {
            latitude: that.data.latitude,
            longitude: that.data.longitude,
          },
          success: res => {
            //console.log(res)
            if (res.result.count == 0) {
              wx.showModal({
                title: '搜索失败',
                content: '非常抱歉，暂没有当前城市的AED信息数据！',
                showCancel: false,
              })
              that.setData({
                modalHidden: true
              })
            } else {
              let count = res.result.count
              let content = '找到' + count + '个附近的AED'
              that.setMarkers(res.result.infos)
              wx.showModal({
                title: '搜索结果',
                content: content,
                showCancel: false,
                success(res) {
                  //console.log(res)
                }
              })
              that.setData({
                modalHidden: true
              })
            }
          }
        })
      }, 2000)

  },
  searchBindtap() {
    this.setData({
      modalHidden: false
    })
    setTimeout(() => {
      console.log("等待2秒，优化搜索体验")
      this.searchAED();
    }, 2000)
  },
  searchAED() {
    var that = this;
    that.setMarkers(null); //重新获取自己的定位
    //每次以地图中心为坐标搜索
    that.mapCtx.getCenterLocation({
      success: e => {
        let lng = e.longitude
        let lat = e.latitude

        qqmapsdk.search({
          keyword: 'aed',
          location: {
            longitude: lng,
            latitude: lat
          },
          success: res => {
           //console.log(res)
            var mark = that.data.markers
            //为了防止获取结果过多，只取前10个定位
            for (let i = 0; i < res.count && i < 10; i++) {
              //console.log(item)
              let content = res.data[i].title + '\n' + res.data[i].address
              mark.push({
                latitude: res.data[i].location.lat,
                longitude: res.data[i].location.lng,
                callout: {
                  content: content,
                  bgColor: "#fff",
                  padding: "5px",
                  borderRadius: "2px",
                  borderWidth: "1px",
                  borderColor: "#07c160",
                },
                iconPath: "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/mapicon/location_red.png?sign=7002919cbb4552d33dbb265ad6c09f6f&t=1618307799"
              }, )
            }
            that.setData({
              markers: mark,
              modalHidden: true
            })
            
            if(that.data.showMarkers==false){
              this.setData({
                showMarkers: true
              })
              if (res.count == 0) {
                wx.showModal({
                  title: '搜索失败',
                  content: '非常抱歉，暂没有当前城市的AED信息数据！',
                  showCancel: false,
                })
              } else {
                let count = res.count
                let content = '找到' + count + '个附近的AED'
                wx.showModal({
                  title: '搜索结果',
                  content: content,
                  showCancel: false,
                })
              }
            }
          }
        })
      }
    })

  },
  //滑动查看周围的AED
  bindRegionChange(e) {
    if (e.type === 'end') {
      //点击搜索之后才会更新位置图标
      if (this.data.showMarkers == true) {
        this.searchAED()
      }
    }
  },
  toPosition() {
    this.myLocation()
    this.mapCtx.moveToLocation();
  },
  myLocation: function () {
    var that = this
    wx.getLocation({
      type: "gcj02",
      success: function (res) {
        //console.log(res.latitude);
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },
  refreshMap() {
    this.onLoad();
    //刷新后隐藏位置图标
    this.setData({
      showMarkers: false
    })
  },
  toUse() {
    wx.navigateTo({
      url: '../AEDuse/AEDuse',
      success: () => {
        wx.setNavigationBarTitle({
          title: 'AED使用教程',
        })
      }
    })
  },
  toReport() {
    wx.navigateTo({
      url: '../newAED/newAED',
      success: () => {
        wx.setNavigationBarTitle({
          title: '公共AED',
        })
      }
    })
  },
  modalCandel() {
    this.setData({
      modalHidden: true
    })
  },
  setMarkers: function (infos) {
    var self = this;
    //console.log(infos)
    if (infos == null) {
      self.setData({
        markers: [{
          latitude: self.data.latitude,
          longitude: self.data.longitude,
          callout: {
            content: "你的位置",
            bgColor: "#fff",
            padding: "5px",
            borderRadius: "2px",
            borderWidth: "1px",
            borderColor: "#07c160",
          },
          iconPath: "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/mapicon/location_green.png?sign=267bfe180042f24a85eb0c69e855c62d&t=1618307898"
        }]
      })
    } else {
      infos.forEach(info => {
        self.data.markers.push(info);
      });
      self.setData({
        markers: self.data.markers
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.mapCtx = wx.createMapContext('mymap')
    that.setData({
      modalHidden: true
    })

    that.myLocation(); //更新自己的定位
    that.setMarkers(null); //设置定位图标
    qqmapsdk = new QQMapWX({
      key: 'GBQBZ-EYTW3-66E3Z-3H4U3-GOXV6-SYFLM' //这里自己的secret秘钥进行填充
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})